package com.buss.controller;

import com.buss.model.UploadFileType;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/")
public class ManageFileTypeController extends BaseFileController {

    @GetMapping("/m")
    public String listUploadedFiles(Model model, UploadFileType form) throws IOException {
        // 查文件类型表
        Example example = new Example(UploadFileType.class);
        // 查询条件拼装
        example.orderBy("moduleCode");
        example.orderBy("id"); //排序(按ID升序)
        List<UploadFileType> listUploadFileType = uploadFileTypeService.selectByExample(example);
        model.addAttribute("lists", listUploadFileType);
        return "manageFileType";
    }


    /**
     * 新增
     */
    @PostMapping("/m")
    public String handleSave(UploadFileType form, RedirectAttributes redirectAttributes) {
        if(StringUtils.isBlank(form.getModuleCode())){
            redirectAttributes.addFlashAttribute("message", "模块编码不能为空!");
            return "redirect:/m/";
        }
        if(StringUtils.isBlank(form.getTypeCode())){
            redirectAttributes.addFlashAttribute("message", "类型编码不能为空!");
            return "redirect:/m/";
        }
        //去掉首尾空格
        form.setModuleCode(form.getModuleCode().trim());
        form.setTypeCode(form.getTypeCode().trim());
        if(StringUtils.isNotBlank(form.getModuleName())){
            form.setModuleName(form.getModuleName().trim());
        }
        if(StringUtils.isNotBlank(form.getTypeName())){
            form.setTypeName(form.getTypeName().trim());
        }
        // 保存
        uploadFileTypeService.insert(form);
        redirectAttributes.addFlashAttribute("message", "新增成功!");
        return "redirect:/m/";
    }

    /**
     * 删除
     */
    @PostMapping("/m/del")
    public String handleDelete(UploadFileType form, RedirectAttributes redirectAttributes) {
        if(ObjectUtils.isEmpty(form.getId())){
            redirectAttributes.addFlashAttribute("message", "id不能为空!");
            return "redirect:/m/";
        }
        uploadFileTypeService.deleteByPrimaryKey(form.getId());
        redirectAttributes.addFlashAttribute("message", "删除成功!");
        return "redirect:/m/";
    }


    /**
     * 编辑跳页
     */
    @PostMapping("/m/uptSel")
    public String handleUpdateSel(UploadFileType form, RedirectAttributes redirectAttributes) {
        if(ObjectUtils.isEmpty(form.getId())){
            redirectAttributes.addFlashAttribute("message", "id不能为空!");
            return "redirect:/m/";
        }
        UploadFileType uploadFileType =
                uploadFileTypeService.selectByPrimaryKey(form.getId());
        redirectAttributes.addFlashAttribute("obj", uploadFileType);
        return "redirect:/m/";
    }


    /**
     * 编辑保存
     */
    @PostMapping("/m/uptSave")
    public String handleUpdateSave(UploadFileType form, RedirectAttributes redirectAttributes) {
        if(StringUtils.isBlank(form.getModuleCode())){
            redirectAttributes.addFlashAttribute("message", "模块编码不能为空!");
            return "redirect:/m/";
        }
        if(StringUtils.isBlank(form.getTypeCode())){
            redirectAttributes.addFlashAttribute("message", "类型编码不能为空!");
            return "redirect:/m/";
        }
        //去掉首尾空格
        form.setModuleCode(form.getModuleCode().trim());
        form.setTypeCode(form.getTypeCode().trim());
        if(StringUtils.isNotBlank(form.getModuleName())){
            form.setModuleName(form.getModuleName().trim());
        }else {
            form.setModuleName("");
        }
        if(StringUtils.isNotBlank(form.getTypeName())){
            form.setTypeName(form.getTypeName().trim());
        }else {
            form.setTypeName("");
        }

        uploadFileTypeService.updateByPrimaryKey(form);
        redirectAttributes.addFlashAttribute("message", "编辑成功!");
        return "redirect:/m/";
    }


    /**
     * 设置档案类别
     * @param modulecode
     * @return
     */
    public String getDalbid(String modulecode){
        String dalbid = "";
        if(modulecode.startsWith("TQ")){
            dalbid = "910";
        }
        return dalbid;
    }

}
